package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.MerGroupInfoMapper;
import cc.rengu.igas.bomp.common.entity.MerGroupInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhangxuran
 * @version 1.0
 * @date 2020/4/20 22:01
 * @description 商户组别信息表操作接口
 */
public class MerGroupInfoMapperImpl implements MerGroupInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);


    @Override
    public List<MerGroupInfo> selectMerGroupInfoByCondition(String grpNo, String grpDesc) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompMerGroupInfoByCondition");
        MerGroupInfo merGroupInfo = new MerGroupInfo();
        if (!StringUtil.isEmptyOrNull(grpNo)) {
            merGroupInfo.setGrpNo(grpNo);
        }

        if (!StringUtil.isEmptyOrNull(grpDesc)) {
            merGroupInfo.setGrpDesc(grpDesc);
        }

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, merGroupInfo, MerGroupInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据组别编号、产品标识、查询商户组别信息失败, grpNo:<{}>,cardInstFlag:<{}>", grpNo, grpDesc);
            return null;
        }
        return objectList.stream().map(item -> (MerGroupInfo) item).collect(Collectors.toList());
    }

    @Override
    public PageResult selectMerGroupInfoPageByCondition(String grpNo, String grpDesc, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompMerGroupInfoByCondition");
        MerGroupInfo merGroupInfo = new MerGroupInfo();
        if (!StringUtil.isEmptyOrNull(grpNo)) {
            merGroupInfo.setGrpNo(grpNo);
        }

        if (!StringUtil.isEmptyOrNull(grpDesc)) {
            merGroupInfo.setGrpDesc(grpDesc);
        }

        return dbsUtil.dbsFetchPageListData(sqlStr, merGroupInfo, MerGroupInfo.class, pageInfo);
    }
}
